#include "StdAfx.h"
#include "RoleMgr.h"
#include "Role.h"

XRoleMgr* g_pRoleMgr = NULL;

XRoleMgr::XRoleMgr(void)
{
}

XRoleMgr::~XRoleMgr(void)
{
}

XRole* XRoleMgr::AddRole(const char szRoleName[], DWORD dwRoleID)
{
    XRole*  pRole       = NULL;
    BOOL    bRetCode    = false;
    DWORD   dwNewRoleID = dwRoleID;

    XYLOG_FAILED_JUMP(szRoleName[0] != '\0');

    if (dwNewRoleID >= m_RoleTable.size())
    {
        m_RoleTable.resize(dwNewRoleID + 10000, NULL);
    }
    else if (dwNewRoleID == 0)
    {
        dwNewRoleID = m_RoleTable.size() + 1;
    }

    pRole = new XRole;

    pRole->m_dwID           = dwNewRoleID;
    pRole->m_nPlaneID       = 1; // Base Plane
    pRole->m_nExp           = 0;
    pRole->m_nLevel         = 1;
    pRole->m_eState         = eRoleState_Offline;
    pRole->m_dwWorldIndex   = 0;
    SafeCopyString(pRole->m_szName, szRoleName);

Exit0:
    return pRole;
}

XRole* XRoleMgr::GetRole(DWORD dwRoleID)
{
    if (dwRoleID >= m_RoleTable.size())
        return NULL;

    return m_RoleTable[dwRoleID];
}

XRole* XRoleMgr::GetRole(const char szRoleName[])
{
    XRoleNameIndex::iterator it = m_RoleNameIndex.find(szRoleName);
    if (it != m_RoleNameIndex.end())
    {
        return it->second;
    }
    return NULL;
}

